#!/bin/bash
# This script installs reaim and runs the regression tests
###SHELLPACK preamble reaim-bench 7.0.1.13

REAIM_FILESIZE=10k
REAIM_POOLSIZE=1m
REAIM_STARTUSERS=1
REAIM_ENDUSERS=$((NUMCPUS*4))
REAIM_INCREMENT=$((NUMCPUS/4))
REAIM_ITERATIONS=5
REAIM_WORKFILE=workfile.fserver

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam   --filesize      REAIM_FILESIZE
###SHELLPACK parseargParam   --poolsize      REAIM_POOLSIZE
###SHELLPACK parseargParam   --startusers    REAIM_STARTUSERS
###SHELLPACK parseargParam   --endusers      REAIM_ENDUSERS
###SHELLPACK parseargParam   --increment     REAIM_INCREMENT
###SHELLPACK parseargParam   --iterations    REAIM_ITERATIONS
###SHELLPACK parseargParam   --workfiles     REAIM_WORKFILES
###SHELLPACK parseargParam   --exclude       REAIM_EXCLUDE
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required reaim-${VERSION}
###SHELLPACK init_complete

for REAIM_WORKFILE in $REAIM_WORKFILES; do
	mmtests_activity $REAIM_WORKFILE
	echo FILESIZE $REAIM_FILESIZE > reaim.config
	echo POOLSIZE $REAIM_POOLSIZE >> reaim.config
	echo DISKDIR $SHELLPACK_DATA  >> reaim.config
	mkdir -p $SHELLPACK_DATA

	# Remove any tests that were requested to be excluded
	cp data/$REAIM_WORKFILE data/${REAIM_WORKFILE}.mmtests
	for EXCLUDE in $REAIM_EXCLUDE; do
		grep -v " $EXCLUDE$" data/${REAIM_WORKFILE}.mmtests > data/${REAIM_WORKFILE}.mmtests.tmp
		mv -f data/${REAIM_WORKFILE}.mmtests.tmp data/${REAIM_WORKFILE}.mmtests
	done
	sed -i -e '/FILESIZE/d' data/${REAIM_WORKFILE}.mmtests
	sed -i -e '/POOLSIZE/d' data/${REAIM_WORKFILE}.mmtests
	mkdir $LOGDIR_RESULTS/$REAIM_WORKFILE

	monitor_pre_hook $LOGDIR_RESULTS $REAIM_WORKFILE
	for ITERATION in `seq 1 $REAIM_ITERATIONS`; do
		echo Running iteration $ITERATION/$REAIM_ITERATIONS workfile $REAIM_WORKFILE
		save_rc ./src/reaim \
			-s$REAIM_STARTUSERS \
			-e$REAIM_ENDUSERS \
			-t \
			-i$REAIM_INCREMENT \
			-fdata/${REAIM_WORKFILE}.mmtests \
			-r1 \
			-c./reaim.config \
			-l$LOGDIR_RESULTS/$REAIM_WORKFILE/reaim.$ITERATION \
				2>&1 | grep -v NSIG | tee $LOGDIR_RESULTS/$REAIM_WORKFILE/reaim.$ITERATION.raw

		recover_rc
		if [ $? -ne 0 ]; then
			die Failed to run reaim
		fi

		echo Syncing...
		$TIME_CMD sync
	done
	monitor_post_hook $LOGDIR_RESULTS $REAIM_WORKFILE
done

exit $SHELLPACK_SUCCESS
